home *** CD-ROM | disk | FTP | other *** search
- Path: erinews.ericsson.se!usenet
- From: Bjorn Fahller <ebcbear@ebc.ericsson.se>
- Newsgroups: comp.lang.c++
- Subject: Re: Creating an object via new with ONLY a pointer to the object
- Date: Thu, 11 Apr 1996 14:37:43 +0200
- Organization: Ericsson Business Networks AB
- Message-ID: <316CFD17.5656@ebc.ericsson.se>
- References: <4kh07v$lno@crchh327.rich.bnr.ca> <4kifrt$lk1@ubszh.fh.zh.ubs.com>
- NNTP-Posting-Host: ebcw050.ebc.ericsson.se
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0 (X11; I; HP-UX A.09.05 9000/735)
-
- Ian Johnston (by ubsswop) wrote:
- >
- > In article <4kh07v$lno@crchh327.rich.bnr.ca>, jobell@bnr.ca (Bret Bieghler) writes:
- > |> An interesting problem I've come across... I was wondering if this
- > |> is possible:
- > |>
- > |> I have a generic CommandObject which defines several pure virtual
- > |> functions. Derived from CommandObject are user commands, such
- > |> as ExitCommand, StatusCommand, etc.
- > |>
- > |> To process a command (currently) I do the following:
- > |>
- > |> CommandObject* basePtr;
- > |>
- > |> if (command == "exit")
- > |> {
- > |> basePtr = new ExitCommand;
- > |> basePtr->implement();
- > |> }
- > |> else if (command == "status")
- > |> {
- > |> basePtr = new StatusCommand;
- > |> basePtr->implement();
- > |> }
- > |>
- > |> What I would LIKE to do is the the following:
- > |>
- > |> CommandObject* basePtr = new commandTable[command];
- > |>
- > |> where commandTable is an associative array as follows:
- > |>
- > |> Key Value
- > |> "exit" ExitCommand*
- > |> "status" StatusCommand*
- >
- > Give each CommadObject class a *static* function:
- >
- > class ExitCommand : public CommandObject
- > {
- > // ...
- > static CommandObject *makeNew();
- > };
- >
- > CommandObject *ExitCommand::makeNew()
- > {
- > return new ExitCommand;
- > }
- >
- > Now you can make a table (assoc array, whatever) of strings and pointers
- > to these functions.
- >
- > typedef CommandObject *(*MakeCmdObj)();
- >
- > MakeCmdObj funcptr = commandTable[command];
- > CommandObject *baseptr = (*funcptr)();
- >
- > Ian
-
- You can make it even slicker by a small template idiom.
-
- Add this:
-
- template <class T>
- class creatable : public CommandObject
- {
- public:
- static CommandObject* create(void);
- };
-
- template <class T>
- CommandObject* creatable<T>::create(void)
- {
- return new T;
- }
-
- Now make your object inherit creatable, instantiated by
- themselves:
-
- class ExitCommand : public creatable<ExitCommand>
- ...
-
-
- I'm not sure if the new rules for overloading on return type
- would allow for releasing creatable from CommandObject (to
- let you multiply inherit it instead.)
-
- The rest of this is handled just in the example given
- above.
- _
- /Bjorn.
- --
- Bjorn Fahller Tel: +46 8 4220898 /
- NA/EBC/FNM/T -------------------
- Ericsson Business Networks AB / "Don't you try to outweird me"
- S-131 89 Stockholm/SWEDEN / -- Zaphod Beeblebrox
-